package com.example.lenovo.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Main3Activity extends AppCompatActivity  {

    private ArrayList<GridViewItemData> mArrayGridViewItemData;


    private GridViewItemData createGridViewItemData(int imgId,String text){
        GridViewItemData gridViewItemData = new GridViewItemData();
        gridViewItemData.imgId = imgId;
        gridViewItemData.text = text;
        return  gridViewItemData;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        GridView gridView = findViewById(R.id.gridView);

        mArrayGridViewItemData = new ArrayList<GridViewItemData>();
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_01,"view_01"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_02,"view_02"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_03,"view_03"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_04,"view_04"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_05,"view_05"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_06,"view_06"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_07,"view_07"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_08,"view_08"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_09,"view_09"));
        mArrayGridViewItemData.add(createGridViewItemData(R.drawable.grid_view_10,"view_10"));

        GridviewActivityItemAdapter gridviewActivityItemAdapter = new GridviewActivityItemAdapter(this);
        gridView.setAdapter(gridviewActivityItemAdapter);
    }

    public class GridviewActivityItemAdapter extends BaseAdapter {



        private Context context;
        private LayoutInflater layoutInflater;

        public GridviewActivityItemAdapter(Context context) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mArrayGridViewItemData.size();
        }

        @Override
        public GridViewItemData getItem(int position) {
            return mArrayGridViewItemData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.gridview, null);
                convertView.setTag(new ViewHolder(convertView));
            }
            initializeViews((GridViewItemData)getItem(position), (ViewHolder) convertView.getTag());
            return convertView;
        }

        private void initializeViews(GridViewItemData object, ViewHolder holder) {
            holder.imageView.setImageResource(object.imgId);
            holder.textView.setText(object.text);
        }

        protected class ViewHolder {
            private ImageView imageView;
            private TextView textView;

            public ViewHolder(View view) {
                imageView = (ImageView) view.findViewById(R.id.imageView);
                textView = (TextView) view.findViewById(R.id.textView);
            }
        }
    }

}
